/*
 * Created by JFormDesigner on Wed Jun 08 19:11:56 CST 2022
 */

package jw3.c10.view;

import java.awt.event.*;

import jw3.c10.controllner.*;
import jw3.c10.model.Userinfo;
import jw3.c10.utils.DBConnection;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class UserM extends JFrame {
    public UserM() {
        initComponents();
        this.setSize(800,800);
        table1.setSize(600,400);
        panel1.setSize(600,400);
        this.setVisible(true);
        dialog1.setSize(500,500);
    }
    public  int showData(String Name){
        String sql="select *from userinfo where UserName like ?";
        data=new Vector<>();
        try {
            Connection conn= DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,"%"+Name+"%");
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();//临时
                data1.add(rs.getInt("UserID"));
                data1.add(rs.getString("UserName"));
                data1.add(rs.getString("Account"));
//            data.add(rs.getInt("pwd"));

                data1.add(rs.getInt("pwd"));
//                data1.add(rs.getInt("Jueisdiction"));
                if (rs.getInt("Jueisdiction")==0){
                    data1.add("管理员");
                }else {
                    data1.add("普通用户");
                }
                data.add(data1);
            }
            DBConnection.close(rs,ps,conn);
            Vector<Object> heads=new Vector<Object>();
            heads.add("编号");
            heads.add("姓名");
            heads.add("账号");
            heads.add("密码");
            heads.add("权限");
            DefaultTableModel dtm=new DefaultTableModel(data,heads);
            table1.setModel(dtm);



        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;

    }
//搜索
    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        showData(textField1.getText());


    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
        //删除
        int index=table1.getSelectedRow();//获取索引
        if (index!=-1){
            if (JOptionPane.showConfirmDialog(null,"是否确定删除？")==0){
String sql="delete from userinfo where UserID=?";
          int id= (int) data.get(index).get(0);
                try {
                    Connection conn=DBConnection.getConnection();
                    PreparedStatement ps=conn.prepareStatement(sql);
                    ps.setInt(1,id);
                    if (ps.executeUpdate()>0){
                        showData("");
                        JOptionPane.showMessageDialog(null,"删除成功");
                    }else {
                        JOptionPane.showMessageDialog(null,"删除失败");
                    }




                } catch (Exception ex) {
                    ex.printStackTrace();
                }


            }
        }else {
            JOptionPane.showMessageDialog(null,"您未选中任意一行");
        }
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
        //改

        int index=table1.getSelectedRow();//选中行的索引
        a= String.valueOf(table1.getValueAt(index,0));
        if (index!=-1){//是否选中了一行
            //获取值 data、index
            //先传值
//            label1.setText(data.get(index).get(0).toString());
            textField7.setText(data.get(index).get(1).toString());
            textField8.setText(data.get(index).get(2).toString());
            textField9.setText(data.get(index).get(3).toString());
            textField10.setText(data.get(index).get(4).toString());

            //后展示对话框
            dialog1.setModal(true);
            dialog1.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(null,
                    "您未选中任意一行!");
        }}

        private void button4MouseClicked(MouseEvent e) {
            // TODO add your code here
            //确认按钮
            Userinfo ui=new Userinfo();
           ui.setUserName(textField7.getText());
           ui.setAccount(textField8.getText());
           ui.setPwd(textField9.getText());
           ui.setJueisdiction(Integer.parseInt(textField10.getText()));
            //2、简单判断


            if (!ui.getAccount().equals("") && !ui.getPwd().equals("")){
                //唯一键重复

                if (UserinfoController.edit(ui)){

                    JOptionPane.showMessageDialog(null,
                            "更新成功!");
                    dialog1.dispose();//关闭对话框 showData("");//自动刷新
                }else{
                    JOptionPane.showMessageDialog(null,
                            "更新失败!");
                }
            }else{
                JOptionPane.showMessageDialog(null,
                        "账号或密码不能为空!");
            }

        }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button3 = new JButton();
        dialog1 = new JDialog();
        label6 = new JLabel();
        textField7 = new JTextField();
        textField8 = new JTextField();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        textField9 = new JTextField();
        textField10 = new JTextField();
        button4 = new JButton();
        button5 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(textField1);
        textField1.setBounds(30, 30, 120, textField1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(175, 30), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(265, 30), button2.getPreferredSize()));

        //======== panel1 ========
        {
            panel1.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(65, 110), panel1.getPreferredSize()));

        //---- button3 ----
        button3.setText("\u4fee\u6539");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(365, 30), button3.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);

            //---- label6 ----
            label6.setText("\u59d3\u540d");
            dialog1ContentPane.add(label6);
            label6.setBounds(105, 15, 50, label6.getPreferredSize().height);
            dialog1ContentPane.add(textField7);
            textField7.setBounds(190, 15, 120, textField7.getPreferredSize().height);
            dialog1ContentPane.add(textField8);
            textField8.setBounds(190, 55, 120, textField8.getPreferredSize().height);

            //---- label7 ----
            label7.setText("\u8d26\u53f7");
            dialog1ContentPane.add(label7);
            label7.setBounds(105, 65, 60, label7.getPreferredSize().height);

            //---- label8 ----
            label8.setText("\u5bc6\u7801");
            dialog1ContentPane.add(label8);
            label8.setBounds(105, 105, 60, label8.getPreferredSize().height);

            //---- label9 ----
            label9.setText("\u6743\u9650");
            dialog1ContentPane.add(label9);
            label9.setBounds(105, 145, 34, label9.getPreferredSize().height);
            dialog1ContentPane.add(textField9);
            textField9.setBounds(190, 100, 120, textField9.getPreferredSize().height);
            dialog1ContentPane.add(textField10);
            textField10.setBounds(190, 140, 120, textField10.getPreferredSize().height);

            //---- button4 ----
            button4.setText("\u786e\u8ba4");
            button4.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button4MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button4);
            button4.setBounds(new Rectangle(new Point(45, 225), button4.getPreferredSize()));

            //---- button5 ----
            button5.setText("\u53d6\u6d88");
            dialog1ContentPane.add(button5);
            button5.setBounds(new Rectangle(new Point(170, 225), button5.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        showData("");
    }
    private  static Vector<Vector<Object>> data;
    public static String a;

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button3;
    private JDialog dialog1;
    private JLabel label6;
    private JTextField textField7;
    private JTextField textField8;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JTextField textField9;
    private JTextField textField10;
    private JButton button4;
    private JButton button5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
